package cn.gok.homework;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.TreeMap;

class BookComparator implements Comparator<Book> {

    @Override
    public int compare(Book o1, Book o2) {
        if (o1.getBid() < o2.getBid()) {
            return -1;
        } else if (o1.getBid() == o2.getBid())
            return 0;
        return 1;
    }
}

class Book {
    private int bid;
    private String bname;
    private float price;

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

}

public class HomeWork6 {
    public static void main(String[] args) {
        TreeMap<Book, String> map = new TreeMap<>(new BookComparator());

        map.put(new Book(1, "老友记", 450), "小林1");
        map.put(new Book(1, "小友记", 220), "小林2");
        map.put(new Book(2, "中友记", 100), "小林3");
        map.put(new Book(3, "男友记", 620), "小林4");
        map.put(new Book(4, "女友记", 520), "小林5");

        map.keySet().stream().forEach(x -> System.out.println(x + "-" + map.get(x)));

        map.entrySet().stream().forEach(System.out::println);

        ArrayList<Book> list = new ArrayList<>();
        list.add(new Book(1, "老友记", 450));
        list.add(new Book(1, "小友记", 220));
        list.add(new Book(2, "中友记", 100));
        list.add(new Book(3, "男友记", 120));
        list.add(new Book(4, "女友记", 50));
        list.stream().filter(i -> i.getPrice() < 150).sorted(new BookComparator()).distinct().forEach(System.out::println);
    }
}